package edu.sctu.wangxiaoqiang.day20150616;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by user25 on 2015/6/16.
 */
public class Net {


    /*
    字节流
     */
    public String urlText(String path){
        int count = 0;
        try {
            URL url = new URL(path);
            byte[] b = new byte[1024];
            try {
                InputStream inputStream = url.openStream();
                while ((count = inputStream.read(b)) !=-1 ){
                    System.out.println(new String(b,0,count,"UTF-8"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

     return path;
    }


    /*
    bufferReader
     */
    public void urlText000(String path){
        int count = 0;
        char[] c = new char[200];
        try {
            URL url = new URL(path);
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
                FileWriter fileWriter = new FileWriter("D:\\AAAA.txt");
                while ((count = bufferedReader.read()) !=-1 ){
                    fileWriter.write(count);
                }
                bufferedReader.close();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }




    /*
    URLconnection
     */
    public void urlTextOne(String path){
        int count = 0;
        try {
            URL url = new URL(path);
            byte[] b = new byte[1024];
            try {
                URLConnection urlConnection = url.openConnection();
                urlConnection.connect();
                InputStream inputStream = urlConnection.getInputStream();
                while ((count = inputStream.read(b)) !=-1 ){
                    System.out.println(new String(b,0,count,"gb2312"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    /*
    HTTPURLconnnection
     */
     public void httpUrlConnection(String  path) {
        String pathUrl = path;// 建立连接
        URL url = null;
        try {
            url = new URL(pathUrl);
            HttpURLConnection httpConn = null;
            try {
                httpConn = (HttpURLConnection) url.openConnection();
                httpConn.setDoOutput(true);// 使用 URL 连接进行输出
                httpConn.setDoInput(true);// 使用 URL 连接进行输入
                httpConn.setUseCaches(false);// 忽略缓存
                httpConn.setRequestMethod("POST");// 设置URL请求方法
//                String requestString = "客服端要以流方式发送到服务端的数据...";
                InputStream inputStream = httpConn.getInputStream();
                inputStream.read();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}
//URL地址头:http://api.ii.com.cn/openoauth
//        参数：code[username]：用户名值huangkai1
//        code[password]：密码值huangkai
//        code[email]：邮箱值huang_k@yeah.net
//c:ii
//        a:regist